package net.lab1024.sa.admin.module.business.category.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lab1024.sa.admin.common.Log;
import net.lab1024.sa.admin.config.UserContextHolder;
import net.lab1024.sa.admin.constant.AdminSwaggerTagConst;
import net.lab1024.sa.admin.module.business.category.domain.dto.UserDTO;
import net.lab1024.sa.admin.module.business.category.domain.dto.UserQueryDTO;
import net.lab1024.sa.admin.module.business.category.domain.entity.UpdatePasswordQuery;
import net.lab1024.sa.admin.module.business.category.domain.entity.User;
import net.lab1024.sa.admin.module.business.category.domain.entity.UserContext;
import net.lab1024.sa.admin.module.business.category.domain.vo.ImportResultVO;
import net.lab1024.sa.admin.module.business.category.domain.vo.UserVO;
import net.lab1024.sa.admin.module.business.category.service.UserService;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import static net.lab1024.sa.base.common.domain.ResponseDTO.OK_CODE;

@Tag(name = AdminSwaggerTagConst.Business.SYNTHESIS_USER)
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 新增用户
    @PostMapping("/add")
    public ResponseDTO<Integer> addUser(@RequestBody UserDTO user) {
        try {
            Integer addResult = userService.addUser(user);
            return ResponseDTO.ok(addResult);
        } catch (Exception e) {
            return new ResponseDTO<>(OK_CODE, null, false, "新增失败", null);
        }
    }

    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) {
        userService.downloadTemplate(response);
    }

    @PostMapping("/import")
    public ResponseDTO<ImportResultVO> importUsers(
            @RequestParam("file") MultipartFile file) {
        ImportResultVO result = userService.importUsers(file);
        ResponseDTO<ImportResultVO> responseDTO = new ResponseDTO<>();
        responseDTO.setOk(false);
        if (result.getErrorList().isEmpty()) {
            responseDTO.setOk(true);
        }
        responseDTO.setData(result);

        return responseDTO;

        //if (result.getErrorList().isEmpty()) {
        //    return ResponseDTO.ok();
        //} else {
        //    return ResponseDTO.error(responseDTO);
        //}

        //        try {
        //            userService.importUsers(file);
        //            return ResponseDTO.ok(true);
        //        } catch (RuntimeException e) {
        //            return ResponseDTO.ok(false);
        //        }
    }

    // 导出用户
    @PostMapping("/export")
    public void exportUsers(@RequestBody UserQueryDTO userQueryDTO, HttpServletResponse response) {
        userService.exportUsers(userQueryDTO, response);
    }

    @PostMapping("/list")
    public ResponseDTO<Page<UserVO>> listUsers(@RequestBody UserQueryDTO query) {
        try {
            return ResponseDTO.ok(userService.listUsers(query));
        } catch (Exception e) {
            return ResponseDTO.error(UserErrorCode.SELECT_USER, e.getMessage());
        }
    }

    @GetMapping("/detail")
    public ResponseDTO<User> getDetail(@RequestParam(required = false) Long id,
                                       @RequestParam(required = false) String userCode) {
        try {
            return ResponseDTO.ok(userService.getDetail(id, userCode));
        } catch (Exception e) {
            return ResponseDTO.error(UserErrorCode.SELECT_USER, e.getMessage());
        }
    }

    @PostMapping("/update")
    public ResponseDTO<Integer> updateUser(@RequestBody UserDTO user) {
        try {
            return ResponseDTO.ok(userService.updateUser(user));
        } catch (Exception e) {
            return ResponseDTO.error(UserErrorCode.UPDATE_USER, e.getMessage());
        }
    }

    @Operation(summary = "修改密码")
    @PostMapping("/password/update")
    public ResponseDTO<String> updatePassword(@RequestBody UpdatePasswordQuery updatePasswordQuery) {
        UserContext userContext = UserContextHolder.getUserContext();
        updatePasswordQuery.setUserId(Long.valueOf(userContext.getUserId()));
        return userService.updatePassword(SmartRequestUtil.getRequestUser(), updatePasswordQuery);
    }

    @Operation(summary = "重置密码")
    @GetMapping("/password/reset")
    public ResponseDTO<String> resetPassword(@Param("userId") Long userId) {
        try {
            Integer integer = userService.resetPassword(userId);
            if (integer > 0) {
                return ResponseDTO.ok();
            }
            return ResponseDTO.ok(false);
        } catch (Exception e) {
            return ResponseDTO.error(UserErrorCode.RESET_PASSWORD, e.getMessage());
        }
    }

}